package my.ezbart;

import org.xmlvm.iphone.CGContext;
import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.UIColor;
import org.xmlvm.iphone.UIGraphics;
import org.xmlvm.iphone.UIImage;
import org.xmlvm.iphone.UILabel;
import org.xmlvm.iphone.UIScreen;
import org.xmlvm.iphone.UIView;

/**
 * @author Cecil Holmes
 * @version 0.1
 * 
 * TrainView.java
 *
 * This is a View class used to display trains found by the Train Retriever class.
 * 
 * View class that displays as a rectangle and draws all necessary information inside
 * using the drawRect function.
 */
public class TrainView extends UIView {
	
	private int ID = -1;
	private String origin = "origin";
	private String destination = "destination";
	private int isTransfer = 0;
	private UIImage t = UIImage.imageNamed("T.gif");
	
	public TrainView() {

	}
	
	public TrainView(int id, String[] newTrain) {
		this.origin = newTrain[0];
		this.destination = newTrain[1];
		this.setFrame(new CGRect(0, (id*121), UIScreen.mainScreen().getApplicationFrame().size.width, 120));
		this.setBackgroundColor(UIColor.whiteColor);
		this.isTransfer = (this.ID=id)%2;
	}
	
	@Override
	public void drawRect(CGRect rect) {
        CGContext ctx = UIGraphics.getCurrentContext();
        UILabel trainLineName = new UILabel();
        UILabel trainLineName2 = new UILabel();
        UILabel transferStation = new UILabel();
        
        UILabel originLabel = new UILabel(new CGRect(5, 50, (UIScreen.mainScreen().getApplicationFrame().size.width-115)/2, 20));
        UILabel destinationLabel = new UILabel(new CGRect(UIScreen.mainScreen().getApplicationFrame().size.width-80, 50, 75, 20));
        UILabel departureTime = new UILabel(new CGRect(5, 90, 75, 20));
        UILabel arrivalTime = new UILabel(new CGRect(UIScreen.mainScreen().getApplicationFrame().size.width-80, 90, 75, 20));
        
        trainLineName.setAdjustsFontSizeToFitWidth(true);
        trainLineName2.setAdjustsFontSizeToFitWidth(true);
        
        //Train Line colors
        float[] pittsburgMilbrae = new float[] { 1, 229f/255f, 0, 1 };
        float[] richmondFremont = new float[] { 249f/255f, 151f/255f, 0, 1 };
        float[] richmondMilbrae = new float[] { 231f/255f, 14f/255f, 28f/255f, 1 };
        //float[] fremontDalyCity = new float[] { 66f/255f, 172f/255f, 48f/255f};
        //float[] dublinDalyCity = new float[] { 0, 156f/255f, 237f/255f, 1 };
        float[] white = new float[] { 1, 1, 1, 1 };

        if (this.ID==0) {
            departureTime.setText("10:55 am");
            arrivalTime.setText("11:35 am");
        } else if (this.ID==1) {
        	departureTime.setText("11:03 am");
            arrivalTime.setText("11:42 am");
        } else if (this.ID==2) {
        	departureTime.setText("11:10 am");
            arrivalTime.setText("11:50 am");
        } else if (this.ID==3) {
        	departureTime.setText("11:18 am");
            arrivalTime.setText("11:57 am");
        }
        /**
         * The color is defined as an array of float values in
         * the interval 0-1 representing an RGBA (Red, Green,
         * Blue, Alpha) value. 
         * 
         * Hint: Red{1,0,0,1} Green{0,1,0,1} Yellow{1,1,0,1} Blue{0,0,1,1} Orange{2,1,0,1}
         */
        
        /**
         *  Train display (No Transfer) 
         */
        if (this.isTransfer == 0) {
            ctx.setFillColor(richmondMilbrae);
            ctx.fillEllipseInRect(new CGRect(20, 30, 16, 16));
            
            ctx.setFillColor(white);
            ctx.fillEllipseInRect(new CGRect(23, 33, 10, 10));
            
            ctx.setFillColor(richmondMilbrae);
            
            //train line
        	trainLineName = new UILabel(new CGRect(35, 5, UIScreen.mainScreen().getApplicationFrame().size.width-75, 20));
            trainLineName.setText("Milbrae");
            trainLineName.setTextAlignment(1);
            trainLineName.setBackgroundColor(UIColor.clearColor);
            ctx.fillRect(new CGRect(35, 36, UIScreen.mainScreen().getApplicationFrame().size.width-75, 4));
            
            //destination circle
            ctx.fillEllipseInRect(new CGRect(UIScreen.mainScreen().getApplicationFrame().size.width-40, 30, 16, 16));

            //origin info
            originLabel.setText(this.origin);
            originLabel.setTextAlignment(1);
            originLabel.setAdjustsFontSizeToFitWidth(true);
            originLabel.setBackgroundColor(UIColor.clearColor);
            
            //departureTime.setText("12:02 pm");
            departureTime.setTextAlignment(1);
            departureTime.setBackgroundColor(UIColor.clearColor);
            
            //destination info
            destinationLabel.setText(this.destination);
            destinationLabel.setTextAlignment(1);
            destinationLabel.setAdjustsFontSizeToFitWidth(true);
            destinationLabel.setBackgroundColor(UIColor.clearColor);
            
            //arrivalTime.setText("1:42 pm");
            arrivalTime.setTextAlignment(1);
            arrivalTime.setBackgroundColor(UIColor.clearColor);
        }
        
        /**
         *  Transfer Train display
         */
        if (this.isTransfer == 1) {
            ctx.setFillColor(richmondFremont);
            ctx.fillEllipseInRect(new CGRect(20, 30, 16, 16));
            
            ctx.setFillColor(white);
            ctx.fillEllipseInRect(new CGRect(23, 33, 10, 10));
            
            ctx.setFillColor(richmondFremont);
            
	        //transfer train line
	        trainLineName = new UILabel(new CGRect(30, 5, (UIScreen.mainScreen().getApplicationFrame().size.width-75)/2, 20));
	        trainLineName.setText("Fremont");
	        trainLineName.setTextAlignment(1);
	        trainLineName.setBackgroundColor(UIColor.clearColor);
	        
	        trainLineName2 = new UILabel(new CGRect(35+(UIScreen.mainScreen().getApplicationFrame().size.width-75)/2, 5,
	        		(UIScreen.mainScreen().getApplicationFrame().size.width-75)/2, 20));
	        trainLineName2.setText("San Francisco Int'l Airport");
	        trainLineName2.setTextAlignment(1);
	        trainLineName2.setBackgroundColor(UIColor.clearColor);
	        
	        ctx.fillRect(new CGRect(35, 36, (UIScreen.mainScreen().getApplicationFrame().size.width-75)/2, 4));
	        
	        ctx.setFillColor(pittsburgMilbrae);
	        ctx.fillRect(new CGRect(35+(UIScreen.mainScreen().getApplicationFrame().size.width-75)/2, 36,
        		(UIScreen.mainScreen().getApplicationFrame().size.width-75)/2, 4));
	        
	        t.drawInRect(new CGRect(35+(UIScreen.mainScreen().getApplicationFrame().size.width-75)/2-(t.getSize().width/2), 
	        		36-(t.getSize().height/2), t.getSize().width, t.getSize().height));
	        
	        transferStation = new UILabel(new CGRect(35, 50, UIScreen.mainScreen().getApplicationFrame().size.width-75, 20));
	        transferStation.setText("MacArthur");
	        transferStation.setTextAlignment(1);
	        transferStation.setAdjustsFontSizeToFitWidth(true);
	        transferStation.setBackgroundColor(UIColor.clearColor);
	        
	        //destination circle
	        ctx.fillEllipseInRect(new CGRect(UIScreen.mainScreen().getApplicationFrame().size.width-40, 30, 16, 16));
	        
	        //origin info
	        originLabel.setText(this.origin);
	        originLabel.setTextAlignment(1);
	        originLabel.setAdjustsFontSizeToFitWidth(true);
	        originLabel.setBackgroundColor(UIColor.clearColor);
	        
	        //departureTime.setText("12:02 pm");
	        departureTime.setTextAlignment(1);
	        departureTime.setBackgroundColor(UIColor.clearColor);
	        
	        //destination info
	        destinationLabel.setText(this.destination);
	        destinationLabel.setTextAlignment(1);
	        destinationLabel.setAdjustsFontSizeToFitWidth(true);
	        destinationLabel.setBackgroundColor(UIColor.clearColor);
	        
	        //arrivalTime.setText("1:42 pm");
	        arrivalTime.setTextAlignment(1);
	        arrivalTime.setBackgroundColor(UIColor.clearColor);
        }
        
        trainLineName.setAdjustsFontSizeToFitWidth(true);
        trainLineName2.setAdjustsFontSizeToFitWidth(true);
        
        this.addSubview(originLabel);
        this.addSubview(destinationLabel);
        this.addSubview(trainLineName);
        if (this.isTransfer == 1) {
        	this.addSubview(trainLineName2);
        	this.addSubview(transferStation);
        }
        this.addSubview(departureTime);
        this.addSubview(arrivalTime);
	}
}